package com.nidu.demo.post.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.company.gateway.CompanyGateway;
import com.nidu.demo.company.model.Company;
import com.nidu.demo.post.convertor.PositionConvertor;
import com.nidu.demo.post.dto.PositionCO;
import com.nidu.demo.post.gateway.PositionGateway;
import com.nidu.demo.post.model.Position;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 职位单查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class PositionSingleQryExe {

    private final PositionGateway gateway;

    private final PositionConvertor convertor;

    private final CompanyGateway companyGateway;

    public SingleResponse<PositionCO> execute(Long id) {
        // 1. 查询数据
        Position post = gateway.getById(id);
        if(post == null){
            throw new BizException(ErrorCodeConstants.POSITION_NOT_FOUND.getCode(), ErrorCodeConstants.POSITION_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        PositionCO postCO = convertor.toClientObject(post);

        Company company = companyGateway.getById(postCO.getCompanyId());
        postCO.setCompanyName(company.getCompanyName());
        return SingleResponse.of(postCO);
    }

}